package dfbz.com.dao;

import dfbz.com.constant.Const;
import dfbz.com.dao.impl.BaseDaoImpl;
import dfbz.com.util.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ArticleDao extends BaseDaoImpl {


    /**
     * 分页查询用户文章信息
     *
     * @param page 当前页
     * @return
     */
    public List<Map<String, Object>> findList(Integer userId, int page, String search) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        List<Map<String, Object>> maps = new ArrayList<>();
        try {
            StringBuilder sql = new StringBuilder();
            sql.append("select id,title,content,browse_count as bcount, publish_date as pdate,");
            sql.append("publish_username as pusername,user_id as uId ");
            sql.append("from article ");
            if (null == search) {
                sql.append("order by pdate desc limit ?,? ");
                maps = qr.query(sql.toString(), new MapListHandler(), (page - 1) * Const.MAX_PAGE_SIZE, Const.MAX_PAGE_SIZE);
            } else {
                sql.append("where title like ? or content like ? ");
                sql.append("order by pdate  desc limit ?,? ");
                search = "%" + search + "%";
                maps = qr.query(sql.toString(), new MapListHandler(), search, search, (page - 1) * Const.MAX_PAGE_SIZE, Const.MAX_PAGE_SIZE);
                System.out.println("sql:"+sql);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return maps;
    }


    /**
     * 查询总用户列表数量
     *
     * @return 总数
     */
    public long getTotalArticle() {
        long total = 0L;
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        try {
            String sql = "select count(*) as total from article ";
            Map<String, Object> map = qr.query(sql, new MapHandler());
            total = Long.parseLong(map.get("total").toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return total;
    }


    public boolean addArticle(String username, Integer uid, String title, String content, String pdate) {

        try {
            QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
            int browserCount = 0;
            String sql = "insert into article(title,content,browse_count ,publish_date,\n" +
                    "publish_username,user_id)\n" +
                    "VALUES\n" +
                    "(?,?,?,?,?,?)";
            qr.update(sql, title, content, browserCount, pdate, username, uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return true;
    }


    public Map<String, Object> findById(Integer id) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Map<String, Object> map=null;
        try {

            StringBuilder sql = new StringBuilder();
            sql.append("select id,title,content,browse_count as bcount, publish_date as pdate,");
            sql.append("publish_username as pusername,user_id as uId ");
            sql.append("from article where id=?");
             map = qr.query(sql.toString(), new MapHandler(), id);
            System.out.println("根据id查询，看看结果" + map);
            return map;
        } catch (SQLException e) {
            e.printStackTrace();

        }

        return map;
    }

    public List<Map<String, Object>> myCollect(int myId,Integer page,String search) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        List<Map<String, Object>> collects = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        try {

            sql.append("select f.u_id,a.id as aId,a.title,a.content,a.browse_count as bcount,a.publish_date as pdate,publish_username as pname,uf.real_name as realname\n");
            sql.append("                from favorite f  \n");
            sql.append("                left join article a on f.a_id=a.id\n");
            sql.append("                left join user_info uf on uf.user_id=a.user_id\n");

           if (null==search){
               sql.append("                where f.u_id=?  and a.user_id!=?  order by aId desc limit ?,?");
               collects = qr.query(sql.toString(),new MapListHandler(), myId,myId,(page - 1) * Const.MAX_PAGE_SIZE, Const.MAX_PAGE_SIZE);

           }else {
               sql.append(  "                where f.u_id=?  and a.user_id!=?  and title like ? order by aId desc");
               search="%"+search+"%";
               collects = qr.query(sql.toString(), new MapListHandler(), myId,myId,search,(page - 1) * Const.MAX_PAGE_SIZE, Const.MAX_PAGE_SIZE);
           }


        } catch (SQLException e) {
            e.printStackTrace();
        }

        return collects;
    }

    public void remove(Integer aId, int userId) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        String sql="delete from article  where id=? and user_id=?";
        System.out.println("sql:"+sql);
        try {
            qr.update(sql,aId,userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }


    public void addBrowseCount(Integer id) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        String sql="update  article  set browse_count=browse_count+1 where id=?";
        System.out.println("sql:"+sql);
        try {
            qr.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
